﻿namespace AnyOffice.DAL
{
    using AnyOffice.Model;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class AssetTypeDao
    {
        public bool Delete(SqlConnection conn, int typeId)
        {
            string commandText = "DELETE FROM [dbo].[Asset_Type] WHERE [TypeId] = @TypeId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@TypeId", SqlDbType.Int) };
            commandParameters[0].Value = typeId;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }

        public int Insert(SqlConnection conn, AssetTypeData data)
        {
            string commandText = "INSERT INTO [dbo].[Asset_Type] ( [TypeNo], [TypeName] ) VALUES ( @TypeNo, @TypeName ) SELECT SCOPE_IDENTITY() AS RetValue";
            SqlParameter[] commandParameters = new SqlParameter[2];
            commandParameters[0] = new SqlParameter("@TypeNo", SqlDbType.Int);
            commandParameters[0].Value = data.TypeNo;
            commandParameters[1] = new SqlParameter("@TypeName", SqlDbType.NVarChar, 50);
            commandParameters[1].Value = data.TypeName;
            object obj2 = SqlHelper.ExecuteScalar(conn, CommandType.Text, commandText, commandParameters);
            if (obj2 != DBNull.Value)
            {
                return Convert.ToInt32(obj2);
            }
            return -1;
        }

        public DataTable SelectAll(SqlConnection conn)
        {
            string commandText = "SELECT [TypeId], [TypeNo], [TypeName] FROM [dbo].[Asset_Type] ORDER BY [TypeNo] ASC";
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText);
            set.Tables[0].TableName = "Asset_Type";
            return set.Tables[0];
        }

        public DataTable SelectByTypeId(SqlConnection conn, int typeId)
        {
            string commandText = "SELECT [TypeId], [TypeNo], [TypeName] FROM [dbo].[Asset_Type] WHERE [TypeId] = @TypeId";
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@TypeId", SqlDbType.Int) };
            commandParameters[0].Value = typeId;
            DataSet set = SqlHelper.ExecuteDataset(conn, CommandType.Text, commandText, commandParameters);
            set.Tables[0].TableName = "Asset_Type";
            return set.Tables[0];
        }

        public bool Update(SqlConnection conn, AssetTypeData data)
        {
            string commandText = "UPDATE [dbo].[Asset_Type] SET [TypeNo] = @TypeNo, [TypeName] = @TypeName WHERE [TypeId] = @TypeId";
            SqlParameter[] commandParameters = new SqlParameter[3];
            commandParameters[0] = new SqlParameter("@TypeId", SqlDbType.Int);
            commandParameters[0].Value = data.TypeId;
            commandParameters[1] = new SqlParameter("@TypeNo", SqlDbType.Int);
            commandParameters[1].Value = data.TypeNo;
            commandParameters[2] = new SqlParameter("@TypeName", SqlDbType.NVarChar, 50);
            commandParameters[2].Value = data.TypeName;
            SqlHelper.ExecuteNonQuery(conn, CommandType.Text, commandText, commandParameters);
            return true;
        }
    }
}

